<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")."GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/json");

require_once 'vars.php';
require_once 'dbCon.php';
require_once 'fileDelete.php';
	
//"prefix" gibt an was gelöscht werden soll
if(isset($_GET['prefix'])) {
	if (isset($_GET['id']) && isset($_GET['path'])) {
		
		$path = $_SERVER['DOCUMENT_ROOT'].$sitePath.'/images'.$_GET['path'];
		$json = array('path' => $path);
		
		if (file_exists($path)) {
			if(rmdirr($path)) {
				$json['result_files'] = 1;
			} else {
				$json['error_files'] = 'delete files failed'; 
				$json['result_files'] = 0;
			}
		} else {
			$json['error_files'] = 'no files associated';
			$json['result_files'] = 0;
		}
		
		$prefix = $_GET['prefix'];
		//Queries erstellen
		switch ($prefix) {
			case 'ctg':
				$queries = array('buttons' => 'DELETE FROM `'.$db.'`.`ak_buttons`'
									.' WHERE btn_category_id = '.$_GET['id']
									.' AND btn_project_id = 0',
								'projects' => 'DELETE FROM `'.$db.'`.`ak_projects`'
									.' WHERE prj_category_id = '.$_GET['id'],
								'categories' => 'DELETE FROM `'.$db.'`.`ak_categories`'
									.' WHERE ctg_id = '.$_GET['id']);
								
				break;
			case 'prj':
				$queries = array('buttons' => 'DELETE FROM `'.$db.'`.`ak_buttons`'
									.' WHERE btn_project_id = '.$_GET['id'],
								'projects' => 'DELETE FROM `'.$db.'`.`ak_projects`'
									.' WHERE prj_id = '.$_GET['id']);
				break;
			case 'btn':
				$queries = array('buttons' => 'DELETE FROM `'.$db.'`.`ak_buttons`'
									.' WHERE btn_id = '.$_GET['id']);
				break;
		}
		foreach($queries as $key => $query) {
			if($result = mysql_query($query)) {
		    	$json['result_'.$key] = 1;
		    	$json['result'] = 1;
		    } else {
		    	$json['error_'.$key] = 'delete '.$key.' failed: '.mysql_error();
		    	$json['error'] = 'delete failed: '.mysql_error();
		    	$json['result'] = 0;
		    }
		}
	} else {
		$json = array('result' => 0,
						'error' => 'Parameter missing! ' 
									.'No "id" and/or no "path" submitted');
	}
} else {
	$json = array('result' => 0,
					'error' => 'Parameter missing! No type submitted!');
}
// Datenbankverbindung schließen
mysql_close($conn);
$encoded = json_encode($json);
echo $encoded;
unset($encoded);
?>